import axios from 'axios'
import { IAnniversary, IAnniversaryCategory } from '@/types/anniversary'
import dayjs from 'dayjs'
import { formatDate, getDateDifference, isBeforeDate } from 'vue3-common/utils/dateUtil'

export const anniversaryCategoryMap: Map<string, IAnniversaryCategory> = new Map([
  ['生活', { name: '生活', title: '🏡 生活', subTitle: '点滴生活，值得铭记', color: '#E6C200' }],
  ['工作', { name: '工作', title: '💼 工作', subTitle: '见证成长，每步精彩', color: '#007BFF' }],
  ['家庭', { name: '家庭', title: '👨‍👩‍👧‍👦 家庭', subTitle: '家人陪伴，温暖常在', color: '#FF5733' }],
  ['成就', { name: '成就', title: '🏆 成就', subTitle: '记录辉煌，见证成长', color: '#28A745' }],
  ['爱情', { name: '爱情', title: '💖 爱情', subTitle: '铭记心动，珍藏爱意', color: '#FF69B4' }],
  ['生日', { name: '生日', title: '🎂 生日', subTitle: '记录成长，珍藏祝福', color: '#FF9900' }],
  ['旅行', { name: '旅行', title: '✈️ 旅行', subTitle: '探索世界，留下足迹', color: '#32CD32' }],
  ['宠物', { name: '宠物', title: '🐾 宠物', subTitle: '陪伴成长，忠实伙伴', color: '#D2691E' }]
])

export const getHolidays = async () => {
  const year = new Date().getFullYear()
  const countryCode = 'CN'
  const apiUrl = `https://date.nager.at/api/v3/PublicHolidays/${year}/${countryCode}`
  const response = await axios.get(apiUrl)
  return response.data
}

export const getAnniversaryRemainDays = (dateItem: IAnniversary): number => {
  if (dateItem.type === 'countUp') {
    return getDateDifference(dateItem.date, formatDate(new Date()), 'days')
  } else if (dateItem.recurring) {
    const currentYear = new Date().getFullYear()
    let thisYear = dayjs(`${currentYear}-${dateItem.date.slice(5)}`)
    if (isBeforeDate(thisYear, formatDate(new Date()))) {
      thisYear = thisYear.add(1, 'year')
    }
    return Math.abs(getDateDifference(thisYear, formatDate(new Date()), 'days'))
  } else {
    return Math.abs(getDateDifference(dateItem.date, formatDate(new Date()), 'days'))
  }
}
